<template>

  <div>
    <!--新增区域-->
    <div class="iRight">
      <el-button type="primary" @click="add">新增</el-button>
    </div>

    <!--搜索区域-->
    <div style="margin: 10px 0">
      <el-input v-model="search" style="width: 20%" placeholder="输入关键字搜索" clearable/>
      <el-button type="primary" style="margin-left: 5px" @click="load">查询</el-button>
    </div>


    <!--剪藏表格-->
    <el-table :data="tableData" border stripe  style="width: 100%">
      <el-table-column property="webName" label="网站名称" width="120px"></el-table-column>
      <el-table-column property="url" label="网址"></el-table-column>
      <el-table-column property="label" label="标签" width="120px"></el-table-column>
      <el-table-column property="introduction" label="简介"></el-table-column>
      <el-table-column property="remark" label="备注"></el-table-column>
      <el-table-column label="操作">
        <template #default="scope">
          <el-button
              @click="handleEdit(scope.row)">编辑
          </el-button>
          <el-popconfirm title="确认删除吗？" hide-icon @confirm="handleDelete(scope.row.id)">
            <template #reference>
              <el-button type="danger">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <!--新增弹框-->
    <el-dialog v-model="dialogVisible" width="40%">

      <el-form :model="form" :label-position="labelPosition" label-width="120px">
        <el-form-item label="网站名称">
          <el-input v-model="form.webName" style="width: 80%"></el-input>
        </el-form-item>
        <el-form-item label="网址">
          <el-input v-model="form.url" style="width: 80%"></el-input>
        </el-form-item>
        <el-form-item label="标签">
          <el-input v-model="form.label" style="width: 80%"></el-input>
        </el-form-item>
        <el-form-item label="简介">
          <el-input v-model="form.introduction" style="width: 80%"></el-input>
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="form.remark" style="width: 80%"></el-input>
        </el-form-item>
      </el-form>


      <template #footer>
    <span class="dialog-footer">
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </span>
      </template>
    </el-dialog>



    <!--分页脚标-->
    <div class="iRight">
      <el-pagination
          :total="total" :current-page="currentPage"
          background layout="prev, pager, next"
          @current-change="handleCurrentChange">
      </el-pagination>
    </div>


  </div>
</template>

<script>
import request from "@/utils/request";

export default {
  name: "Recommended",
  components: {},

  data() {
    return {
      form: {},
      dialogVisible: false,
      search: '',
      currentPage: 1,
      pageSize: 10,
      total: 0,
      tableData: [],
      labelPosition: 'left',
    }
  },

  created() {
    this.load()
  },

  methods: {
    //编辑
    handleEdit(row) {
      this.form = JSON.parse(JSON.stringify(row))
      this.dialogVisible = true
      this.$nextTick(() => {
        if (this.$refs['upload']){
          this.$refs['upload'].clearFiles() //清除历史文件列表
        }
      })
    },
    //删除
    handleDelete(id) {
      console.log(id);
      request.delete("/web/" + id).then(res => {
        if (res.code === '0') {
          this.$message({
            type: "success",
            message: "删除成功"
          })
        } else {
          this.$message({
            type: "error",
            message: res.msg
          })
        }
        this.load() //删除完成之后刷新表格数据
      })
    },
    //改变当前页
    handleCurrentChange(pageNum) {
      this.currentPage = pageNum
      this.load()
    },
    //新增
    add() {
      this.dialogVisible = true
      this.form = {}
      if (this.$refs['upload']){
        this.$refs['upload'].clearFiles() //清除历史文件列表
      }
    },
    //保存
    save() {
      if (this.form.id) {
        //有id则更新
        request.put("/web", this.form).then(res => {
          console.log(res)
          if (res.code === '0') {
            this.$message({
              type: "success",
              message: "更新成功"
            })
          } else {
            this.$message({
              type: "error",
              message: res.msg
            })
          }
          this.load() //更新完成之后刷新表格数据
          this.dialogVisible = false //并且关闭弹窗
        })
      } else {
        //没有id则新增
        request.post("/web", this.form).then(res => {
          console.log(res)
          if (res.code === '0') {
            this.$message({
              type: "success",
              message: "新增成功"
            })
          } else {
            this.$message({
              type: "error",
              message: res.msg
            })
          }
          this.load() //新增完成之后刷新表格数据
          this.dialogVisible = false //并且关闭弹窗
        })
      }
    },

    //查询
    load() {
      request.get("/web", {
        params: {
          pageNum: this.currentPage,
          search: this.search,
        }
      }).then(res => {
        console.log(res)
        this.tableData = res.data.records
        this.total = res.data.total
      })
    },
  }
}
</script>

<style scoped>
/*.iRight {*/
/*  margin: 10px 0;*/
/*  position: absolute;*/
/*  right: 10px;*/
/*  text-align: center;*/
/*}*/
</style>